package com.ms.oschina.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.view.animation.CycleInterpolator;
import android.widget.ImageView;

import com.ms.oschina.R;
import com.ms.oschina.base.BaseActivity;
import com.ms.oschina.util.ShakeListener;
import com.ms.oschina.util.ShakeListener.OnShakeListener;
import com.ms.oschina.util.UiUtils;
import com.nineoldandroids.view.ViewPropertyAnimator;

public class ShakeItActivity extends BaseActivity {

	private ImageView ivShakeit;
	private ShakeListener mShakeListener;

	/**
	 * Find the Views in the layout<br />
	 * <br />
	 * Auto-created  by Android Layout Finder
	 * (http://www.buzzingandroid.com/tools/android-layout-finder)
	 */
	private void findViews() {
		ivShakeit = (ImageView) findViewById(R.id.iv_shakeit);
	}
	@Override
	protected int getLayoutId() {
		return R.layout.activity_shakeit;
	}

	@Override
	protected void initView() {
		findViews();
	}

	@Override
	protected void initData() {
		mShakeListener = new ShakeListener(this);
		mShakeListener.setOnShakeListener(new OnShakeListener() {
			@Override
			public void onShake() {
				Vibrator vibrator = (Vibrator) ShakeItActivity.this
						.getSystemService(Context.VIBRATOR_SERVICE);
				long[] pattern = { 100, 400 }; // 停止 开启 停止 开启
				vibrator.vibrate(pattern, -1);
				// //重复两次上面的pattern
				// 如果只想震动一次，index设为-1
				UiUtils.showToast("都抱紧了");
				
				ViewPropertyAnimator.animate(ivShakeit).translationXBy(50)
				.setInterpolator(new CycleInterpolator(4))
				.setDuration(1000)
				.start();
			}
		});
	}

	@Override
	public void onPause() {
		if (mShakeListener != null) {
			mShakeListener.pause();
		}
		super.onPause();
	}

	@Override
	public void onResume() {
		if (mShakeListener != null) {
			mShakeListener.resume();
		}
		super.onResume();
	}
	

	@Override
	protected void initEvent() {
	}

	@Override
	protected CharSequence getBackActionBarTitle() {
		return UiUtils.getString(R.string.actionbar_title_shake);
	}

}
